// pages/vip/vip.js
import request from "../../api/request"
import {liulan} from "../../api/vipReq"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipList:[],
    freeList:[],
    likeList:[],
    jingpinList:[],
    videoData:[],
    current:0,
    userInfo:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //得到视频时长
  time: 0,
  bindloadedmetadata(e) {
      this.time = ++this.time
      let id = e.mark.id
      let time = e.detail.duration
      const m = parseInt(time / 60)
      const s = parseInt(time % 60)
      time = 0 + m + ":" + s
      const res1 = this.data.vipList.map(v => {
          if (v._id == id) {
              v.time = time
          }
          return v
      });
      const res2 = this.data.freeList.map(v => {
          if (v._id == id) {
              v.time = time
          }
          return v
      });
      const res3 = this.data.likeList.map(v => {
          if (v._id == id) {
              v.time = time
          }
          return v
      });

     const max= Math.max(this.data.vipList.length,this.data.freeList.length,this.data.likeList.length);
      if (this.time == max) {
          this.setData({
            vipList: res1
          });
          this.setData({
            freeList: res2
          });
          this.setData({
            likeList: res3
          });
      }
  },
  changeTab(event){
    const current=event.currentTarget.dataset.id;
    this.setData({current})
  },
  changeSwiper(obj){
    const current=obj.detail.current;
    this.setData({current})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  toMyvip(){
    wx.navigateTo({
      url: '/pages/vip/myvip/myvip'
    })
  },

 async toDetail(event){
    const id=event.currentTarget.dataset.id;
   if(getApp().globalData.userInfo){
    const res=await liulan({
      user_id:getApp().globalData.userInfo._id,
      menu_id:id
    });
   }
    wx.navigateTo({
      url: '/pages/vip/details/details?id='+id,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo:getApp().globalData.userInfo
    });
        //获取网络数据
        request({
          url:"/vip/getRecommendMenuList",
          method:"GET",
          success:res=>{
             if(res.statusCode===200){
              this.setData({
                vipList:res.data.menus    
                })
             }
          }
      });
        request({
          url:"/vip/getisFreeMenuList",
          method:"GET",
          success:res=>{
             if(res.statusCode===200){
              this.setData({
                freeList:res.data.menus    
                })
             }
          }
      });
        request({
          url:"/vip/likeMenu",
          method:"GET",
          success:res=>{
             if(res.statusCode===200){
              this.setData({
                likeList:res.data.menus    
                })
             }
          }
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})